<?php

/**
 * Name: 班长审核记录仓储层
 * User: Silent
 * Date: 2025-11-07
 * Time: 15:30:00
 */

namespace App\Repositories\Checkin;

use App\Models\Checkin\LeaderReview;
use App\Repositories\BaseRepository;
use Illuminate\Support\Arr;

class LeaderReviewRepository extends BaseRepository
{
    public function __construct(LeaderReview $model)
    {
        parent::__construct($model);
    }

    /**
     * 创建或更新记录
     */
    public function updateOrCreate(array $attributes, array $values = [])
    {
        return $this->model->updateOrCreate($attributes, $values);
    }

    /**
     * 检查班长今天是否已经审核过
     */
    public function hasReviewedToday($leaderId, $teamName, $date = null)
    {
        $date = $date ?: today();

        return $this->query()
            ->where('leader_id', $leaderId)
            ->where('team_name', $teamName)
            ->where('review_date', $date)
            ->exists();
    }

    /**
     * 获取班长的审核记录
     */
    public function getLeaderReview($teamName, $date)
    {
        return $this->query()
            ->with('leader')
            ->where('team_name', $teamName)
            ->where('review_date', $date)
            ->first();
    }

    /**
     * 创建或更新班长审核记录
     */
    public function createOrUpdateReview($leaderData, $reviewData, $date = null)
    {
        $date = $date ?: today();

        $signIds = isset($reviewData['sign_ids'])
            ? array_map('intval', explode(',', $reviewData['sign_ids']))
            : [];

        $data = array_merge(Arr::except($reviewData, ['date']), [
            'review_date' => $date,
            'team_name' => $leaderData['team'],
            'leader_id' => $leaderData['id'],
            'leader_name' => $leaderData['name'],
            'sign_ids' => json_encode($signIds),
            'reviewed_at' => now(),
        ]);

        return $this->updateOrCreate([
            'team_name' => $leaderData['team'],
            'review_date' => $date,
        ], $data);
    }

    /**
     * 获取待审核的班组列表
     */
    public function getPendingReviews($date = null)
    {
        $date = $date ?: today();

        return $this->query()
            ->with('leader')
            ->where('review_date', $date)
            ->where('review_status', LeaderReview::STATUS_PENDING)
            ->get();
    }

    /**
     * 获取班长的审核记录列表
     */
    public function getLeaderReviews($leaderId, $perPage = 15, $filters = [])
    {
        $query = $this->query()->with('leader')->where('leader_id', $leaderId);

        // 日期筛选
        if (isset($filters['start_date'])) {
            $query->where('review_date', '>=', $filters['start_date']);
        }
        if (isset($filters['end_date'])) {
            $query->where('review_date', '<=', $filters['end_date']);
        }

        // 状态筛选
        if (isset($filters['status'])) {
            $query->where('review_status', $filters['status']);
        }

        return $query->orderBy('review_date', 'desc')
                    ->paginate($perPage);
    }

    /**
     * 获取指定日期的所有班长审核记录
     */
    public function getDailyReviews($date)
    {
        return $this->query()
            ->where('review_date', $date)
            ->with('leader')
            ->get();
    }

    /**
     * 获取异常审核记录
     */
    public function getAbnormalReviews($date, $teamName = null)
    {
        $query = $this->query()
            ->with('leader')
            ->where('review_date', $date)
            ->where('review_status', LeaderReview::STATUS_REJECTED);

        if ($teamName) {
            $query->where('team_name', $teamName);
        }

        return $query->get();
    }
}

